﻿using MediatR;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.DTOs.WhiteStripWeighingHtmlGroup;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.AdmissionRegistrationFile
{
    public class SearchGroupHander : IRequestHandler<SearchGroupCommand, Resqults<SpliteGroupDTO>>
    {
        private readonly IBateRepository<Partition> part;
        public SearchGroupHander(IBateRepository<Partition> part)
        {
            this.part = part;
        }
        public async Task<Resqults<SpliteGroupDTO>> Handle(SearchGroupCommand request, CancellationToken cancellationToken)
        {
			try
			{
                Resqults<SpliteGroupDTO> res = new Resqults<SpliteGroupDTO>();
                int years = DateTime.Now.Year;
                int months = DateTime.Now.Month;
                if (request.Date != null)
                {
                    years = request.Date.Value.Year;
                    months = request.Date.Value.Month;
                }
                var list = from a in part.GetAll()
                           where (a.PartitionDate.Year == years && a.PartitionDate.Month == months)
                           group new { a } by new { a.PartitionDate.Day } into c
                           select new SpliteGroupDTO()
                           {
                               AvgWight=c.Average(x=>x.a.NetWeight),
                               SumNum = c.Sum(x => x.a.PartitionNum),
                               SumWeight = c.Sum(x => x.a.PartitionNum * x.a.NetWeight),
                               Day = c.Key.Day
                           };
                res.Date=list.ToList();
                return res;
            }
			catch (Exception)
			{

				throw;
			}
        }
    }
}
